package com.example.moblie203zlw_databases11_05.slice;


import com.example.moblie203zlw_databases11_05.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.rdb.ValuesBucket;

public class MainAbilitySlice extends AbilitySlice {
    Image image_add,image_menu;
    DatabaseHelper databaseHelper;
    RdbStore rdbStore;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        databaseHelper=new DatabaseHelper(this);
        image_add= (Image) findComponentById(ResourceTable.Id_image_add);
        image_menu= (Image) findComponentById(ResourceTable.Id_image_menu);
        StoreConfig storeConfig=StoreConfig.newDefaultConfig("student.db");
        RdbOpenCallback rdbOpenCallback=new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                rdbStore.executeSql("create table mobile(name varchar(10) not null ,sex varchar(3) default '保密',phone int not null)");
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        };
        rdbStore=databaseHelper.getRdbStore(storeConfig,1,rdbOpenCallback);
        image_add.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CommonDialog dialog=new CommonDialog(MainAbilitySlice.this);
                Component component_dialog= LayoutScatter.getInstance(MainAbilitySlice.this).parse(
                        ResourceTable.Layout_dialog_add_contacts,null,true
                );
                Button btnConfirm = (Button)
                        component_dialog.findComponentById(ResourceTable. Id_btn_confirm );
                Button btnCacel = (Button)
                        component_dialog.findComponentById(ResourceTable. Id_btn_cancel );
                TextField  nameTf = (TextField)
                        component_dialog.findComponentById(ResourceTable. Id_tf_name );
                TextField  phoneTf = (TextField)
                        component_dialog.findComponentById(ResourceTable. Id_tf_phone );
                RadioContainer  genderRc = (RadioContainer)
                        component_dialog.findComponentById(ResourceTable. Id_rc_gender );
                RadioButton  manRb = (RadioButton)
                        component_dialog.findComponentById(ResourceTable. Id_rb_man );
                RadioButton  ladyRb = (RadioButton)
                        component_dialog.findComponentById(ResourceTable. Id_rb_lady );
                manRb.setChecked(true);
                btnConfirm.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        new ToastDialog(MainAbilitySlice.this).setText("确定").show();
                        dialog.remove();
                    }
                });
                btnCacel.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        new ToastDialog(MainAbilitySlice.this).setText("取消").show();
                        dialog.remove();
                    }
                });
                dialog.setContentCustomComponent(component_dialog);
                dialog.show();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
